package edu.bbu.bibliospring.api.cdiassembler;

import edu.bbu.bibliospring.api.resources.TestResource;
import edu.bbu.bibliospring.backend.model.Book;
import edu.bbu.bibliospring.common.dto.AuthorDto;
import edu.bbu.bibliospring.common.dto.BookDto;
import org.apache.log4j.Logger;

/**
 * Created by erika on 6/13/14.
 * Assembler class transforming the model and dto of the Book entity
 */
public class BookAssembler implements Assembler<Book, BookDto> {

    private static final Logger LOG = Logger.getLogger(TestResource.class);

    @Override
    public Book createModel() {
        LOG.info("BookAssembler - new Book created");
        return new Book();
    }

    @Override
    public BookDto createDto() {
        LOG.info("BookAssembler - new BookDto created");
        return new BookDto(Book.class.getName());
    }

    @Override
    public BookDto modelToDto(Book model) {
        LOG.info("BookAssembler - modelToDto");
        BookDto bookDto = createDto();
        AuthorAssembler authorAssembler = new AuthorAssembler();
        bookDto.setAuthor(authorAssembler.modelToDto(model.getAuthor()));
        bookDto.setTitle(model.getTitle());
        bookDto.setId(model.getId());
        bookDto.setEditor(model.getEditor());
        bookDto.setYearOfPublishing(model.getYearOfPublishing());
        return bookDto;
    }

    @Override
    public Book dtoToModel(BookDto dto) {
        LOG.info("BookAssembler - dtoToModel");
        Book book = createModel();
        AuthorAssembler authorAssembler = new AuthorAssembler();
        book.setId(dto.getId());
        book.setYearOfPublishing(dto.getYearOfPublishing());
        book.setEditor(dto.getEditor());
        book.setTitle(dto.getTitle());
        book.setAuthor(authorAssembler.dtoToModel(dto.getAuthor()));
        return book;
    }
}
